#pragma once

#include<vector>
#include<assert.h>

namespace dan
{
	template<class T>
	class Less
	{
	public:
		bool operator()(const T& a, const T& b)const
		{
			return a < b;
		}
	};

	template<class T>
	class Greater
	{
	public:
		bool operator()(const T& a, const T& b)const
		{
			return a > b;
		}
	};

	template<class T,class Container=vector<T>,class Compare=Less<T>>
	class priority_queue
	{
	public:
		void push(const T&val)
		{
			_con.push_back(val);

			adjustup(_con.size()-1);
		}

		void pop()
		{
			assert(!_con.empty());

			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();

			adjustdown(0);
		}

		const T& top()const
		{
			return _con[0];
		}

		size_t size()const
		{
			return _con.size();
		}

		bool empty()const
		{
			return _con.empty();
		}

		void adjustup(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child>0&&com(_con[parent] , _con[child]))
			{
				swap(_con[child], _con[parent]);
				child = parent;
				parent = (child - 1) / 2;
			}
		}

		void adjustdown(int parent)
		{
			Compare com;
			int child = parent *2 +1;
			while (child < _con.size())
			{
				if (child + 1 < _con.size() && com(_con[child] , _con[child + 1]))
				{
					child++;
				}

				if (com(_con[parent] , _con[child]))
				{
					swap(_con[child], _con[parent]);
				}
				else {
					break;
				}
				parent = child;
				child = parent * 2 + 1;
			}
		}
	private:
		Container _con;
	};
}